﻿(function ()
{
    'use strict';

    angular
        .module('buildsApp')
        .controller('buildsController', buildsController);

    buildsController.$inject = ['$scope', '$sce', 'Builds'];

    function buildsController($scope, $sce, Builds)
    {
        $scope.builds = [];//Builds.query();
        $scope.buildLog = [];
        $scope.buildLog.dataLoading = false;

        $scope.trustSrc = function (src)
        {
            return $sce.trustAsResourceUrl(src);
        };

        $scope.Refresh = function (buildsLoadedCallback)
        {
            Builds.query().$promise.then(function (data)
            {
                $scope.builds = data;

                if (buildsLoadedCallback)
                    buildsLoadedCallback();
            });
        };

        $scope.retry = function (buildNumber)
        {
            Builds.retry('\'' + buildNumber + '\'').$promise.then(function () { window.setTimeout($scope.Refresh, 5000); });
        };

        $scope.getbuildlog = function (buildNumber)
        {
            if (!buildNumber)
            {
                $scope.buildLog = { Status: '', URL: 'about:blank', LogEntries: []};
                return;
            }

            $scope.buildLog.dataLoading = true;
            Builds.getbuildlog('\'' + buildNumber + '\'').$promise.then(
                                                                            function (data)
                                                                            {
                                                                                $scope.buildLog = data;
                                                                            }).finally(function()
                                                                            {
                                                                                $scope.buildLog.dataLoading = false;
                                                                            }
                                                                        );
        };
    }
})()